package com.wangxi.boot10pro.controller;

import com.wangxi.boot10pro.entity.Product;
import com.wangxi.boot10pro.mapper.ProMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProController {
    @Autowired(required = false)
    ProMapper mapper;

    @RequestMapping("/insertPro")
    public int insertPro(@RequestBody Product product){
        System.out.println("product = " + product);
        Product p = mapper.selectByTitle(product.getTitle());
        if(p==null){
            mapper.insertPro(product);
            return 1;
        }
        return 2;
    }

    @RequestMapping("/selectAll")
    public List<Product> selectAll(){
        return mapper.selectAll();
    }

    @RequestMapping("/deleteById")
    public void deleteById(int id){
        mapper.deleteById(id);
    }

    @RequestMapping("/selectPro")
    public Product selectPro(int id){
        return mapper.selectPro(id);
    }

    @RequestMapping("/updatePro")
    public void updatePro(@RequestBody Product product){
        mapper.updatePro(product);
    }
}
